package com.azlogica.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class FrameDaoAdapter {
	private static final String DATABASE_NAME = "frame.db";
	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_TABLE = "frame";
	
	private static final String KEY_ID = "_id";
	private static final String KEY_HEADER = "frame_header";
	private static final String KEY_REPORT = "frame_report";
	private static final String KEY_SACK = "frame_sack";
	private static final String KEY_MESSTYPE = "frame_messtype";
	private static final String KEY_IMEI = "frame_imei";
	private static final String KEY_STATUS = "frame_status";
	private static final String KEY_NUMBER = "frame_number";
	private static final String KEY_ACCURACY = "frame_accuracy";
	private static final String KEY_SPEED = "frame_speed";
	private static final String KEY_AZIMUTH = "frame_azimuth";
	private static final String KEY_ALTITUDE = "frame_altitude";
	private static final String KEY_LONGITUDE = "frame_longitude";
	private static final String KEY_LATITUDE = "frame_latitude";
	private static final String KEY_GPS_UTC = "frame_gps_utc";
	private static final String KEY_MCC = "frame_mcc";
	private static final String KEY_MNC = "frame_mnc";
	private static final String KEY_LAC = "frame_lac";
	private static final String KEY_CLL_ID = "frame_cllId";
	private static final String KEY_BATTERY = "frame_battery";
	private static final String KEY_SEND_TIME = "frame_sendTime";
	private static final String KEY_SERIAL = "frame_serial";
	private static final String KEY_SEND = "frame_send";
	
	
	
	private static final String[] columns = { KEY_ID, KEY_HEADER, KEY_REPORT,
			KEY_SACK, KEY_MESSTYPE, KEY_IMEI, KEY_STATUS, KEY_NUMBER, KEY_ACCURACY,
			KEY_SPEED, KEY_AZIMUTH, KEY_ALTITUDE, KEY_LONGITUDE, KEY_LATITUDE,
			KEY_GPS_UTC, KEY_MCC, KEY_MNC, KEY_LAC, KEY_CLL_ID, KEY_BATTERY, KEY_SEND_TIME, KEY_SERIAL, KEY_SEND };

	private static final String COW_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS "
			+ DATABASE_TABLE
			+ " ("
			+ KEY_ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT,"
			+ KEY_HEADER
			+ " TEXT,"
			+ KEY_REPORT
			+ " TEXT,"
			+ KEY_SACK
			+ " TEXT,"
			+ KEY_MESSTYPE
			+ " TEXT,"
			+ KEY_IMEI
			+ " TEXT,"
			+ KEY_STATUS
			+ " TEXT,"
			+ KEY_NUMBER
			+ " TEXT,"
			+ KEY_ACCURACY
			+ " TEXT,"
			+ KEY_SPEED
			+ " TEXT,"
			+ KEY_AZIMUTH
			+ " TEXT,"
			+ KEY_ALTITUDE
			+ " TEXT,"
			+ KEY_LONGITUDE
			+ " TEXT,"
			+ KEY_LATITUDE
			+ " TEXT,"
			+ KEY_GPS_UTC
			+ " TEXT,"
			+ KEY_MCC
			+ " TEXT,"
			+ KEY_MNC
			+ " TEXT,"
			+ KEY_LAC
			+ " TEXT,"
			+ KEY_CLL_ID
			+ " TEXT,"
			+ KEY_BATTERY
			+ " TEXT,"
			+ KEY_SEND_TIME
			+ " TEXT,"
			+ KEY_SERIAL
			+ " TEXT"
			+ KEY_SEND
			+ " INTEGER);";

	private FrameDBHelper ourHelper;
	private final Context ourContext;
	private SQLiteDatabase ourDatabase;

	private static class FrameDBHelper extends SQLiteOpenHelper {

		public FrameDBHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(COW_CREATE_TABLE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			onCreate(db);
		}

		@Override
		public void onOpen(SQLiteDatabase _db) {
			_db.execSQL(COW_CREATE_TABLE);
		}

		@Override
		public void onDowngrade(SQLiteDatabase db, int oldVersion,
				int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			onCreate(db);
		}

	}

	public FrameDaoAdapter(Context c) {
		ourContext = c;
	}

	public FrameDaoAdapter open() throws SQLException {
		ourHelper = new FrameDBHelper(ourContext);
		ourDatabase = ourHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		ourHelper.close();
	}

	public long createFrame(String header, String report, String sack, String messType, 
			String imei, String status, String number, String accuracy, String speed, String azimuth, 
			String altitude, String longitude, String latitude, String gpsUtc,
			String mcc, String mnc, String lac, String cllId, String battery, String sendTime)
			throws SQLException {
		ContentValues cv = new ContentValues();
		cv.put(KEY_HEADER, header);
		cv.put(KEY_REPORT, report);
		cv.put(KEY_SACK, sack);
		cv.put(KEY_MESSTYPE, messType);
		cv.put(KEY_IMEI, imei);
		cv.put(KEY_STATUS, imei);
		cv.put(KEY_NUMBER, number);
		cv.put(KEY_ACCURACY, accuracy);
		cv.put(KEY_SPEED, speed);
		cv.put(KEY_AZIMUTH, azimuth);
		cv.put(KEY_ALTITUDE, altitude);
		cv.put(KEY_LONGITUDE, longitude);
		cv.put(KEY_LATITUDE, latitude);
		cv.put(KEY_GPS_UTC, gpsUtc);
		cv.put(KEY_MCC, mcc);
		cv.put(KEY_MNC, mnc);
		cv.put(KEY_LAC, lac);
		cv.put(KEY_CLL_ID, cllId);
		cv.put(KEY_BATTERY, battery);
		cv.put(KEY_SEND_TIME, sendTime);
		return ourDatabase.insert(DATABASE_TABLE, null, cv);
	}

	public Cursor readFrame() throws SQLException {
		return ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
				null, null);
	}

	public Cursor searchFrame(int id) throws SQLException {
		return ourDatabase.query(DATABASE_TABLE, columns, KEY_ID + "=" + id,
				null, null, null, null);
	}

	public void deleteFrame(int id) throws SQLException {
		ourDatabase.delete(DATABASE_TABLE, KEY_ID + "=" + id, null);
	}

	public void updateFrame(int id, String header, String report, String sack, String messType, 
			String imei, String status, String number, String accuracy, String speed, String azimuth, 
			String altitude, String longitude, String latitude, String gpsUtc,
			String mcc, String mnc, String lac, String cllId, String battery, String sendTime) throws SQLException {
		ContentValues cv = new ContentValues();
		cv.put(KEY_HEADER, header);
		cv.put(KEY_REPORT, report);
		cv.put(KEY_SACK, sack);
		cv.put(KEY_MESSTYPE, messType);
		cv.put(KEY_IMEI, imei);
		cv.put(KEY_STATUS, imei);
		cv.put(KEY_NUMBER, number);
		cv.put(KEY_ACCURACY, accuracy);
		cv.put(KEY_SPEED, speed);
		cv.put(KEY_AZIMUTH, azimuth);
		cv.put(KEY_ALTITUDE, altitude);
		cv.put(KEY_LONGITUDE, longitude);
		cv.put(KEY_LATITUDE, latitude);
		cv.put(KEY_GPS_UTC, gpsUtc);
		cv.put(KEY_MCC, mcc);
		cv.put(KEY_MNC, mnc);
		cv.put(KEY_LAC, lac);
		cv.put(KEY_CLL_ID, cllId);
		cv.put(KEY_BATTERY, battery);
		cv.put(KEY_SEND_TIME, sendTime);
		ourDatabase.update(DATABASE_TABLE, cv, KEY_ID + "=" + id, null);
	}

	public Cursor readLastFrame() {
		return ourDatabase.query(DATABASE_TABLE, columns, KEY_ID
				+ "= (SELECT MAX (" + KEY_ID + ") FROM " + DATABASE_TABLE
				+ ");", null, null, null, null);
	}
}